define(function () {
    return function (pane) {
        /**
         * 导航
         */
        {
            pane.html(utils.getHtml('default_table'));

            utils.addFilterDropdown(pane, {
                name: 'type',
                dropdown: dicts.dict_type
            });
        }

        //全局变量
        var comment_fields = utils.convertFields([
            ['type', 'select', '类别', false, dicts.dict_type],
            ['order_by', 'text', '排序', {notEmpty: {message: '排序不能为空'}}],
            ['value', 'text', '字典值', {notEmpty: {message: '名字不能为空'}}],
            ['text', 'text', '文本', {notEmpty: {message: '字典值不能为空'}}],
            ['icon', 'text', '图标'],
            ['color', 'text', '颜色']
        ]);

        //初始化工具
        utils.addTools(pane, [{className: 'add-dict-btn', icon: 'fa-plus', text: '添加字典'}]);
        $(pane).on('click', '.add-dict-btn', function () {
            utils.formAdd(table, {
                name: '字典', fields: comment_fields, columns: 6,
                fnServerData: function (paramData) {
                    return adminApi('dicts_add', paramData);
                }
            }).then(function (form) {
                $('select[name="type"]', form).val(utils.filterValue(pane, 'type'));
            });
        });

        //初始化表格
        var table = utils.dataTable(pane, {
            columns_arrays: [
                ["order_by", "排序", false],
                ["id", "编号"],
                ["value", "字典值", false],
                ["text", "文本", false, function (value, type, row) {
                    if (!row.color) {
                        return value;
                    }
                    return '<span class="badge" style="background-color: ' + row.color + '!important;">' + value + '</span>';
                }],
                ["icon", "图标", false, utils.imageRender()],
                ["feature", "推荐", false, utils.dictRender('boolean')],
                ["updated_at", "更新时间", false, utils.dateTimeRender()]
            ],
            sorting: [[0, 'asc']],
            rowReorder: {
                update: false
            },
            columnDefs: [{orderable: true, className: 'reorder', targets: 0}],
            fnServerData: function (paramData) {
                return adminApi('dicts_browse', paramData);
            }
        });
        table.on('row-reorder.dt', function (e, diff) {
            var sorts = [];
            for (var i = 0, ien = diff.length; i < ien; i++) {
                var rowData = table.api().row(diff[i].node).data();
                sorts.push({order_by: diff[i].newPosition + 1, id: rowData.id});
            }
            adminApi('dicts_sort', JSON.stringify(sorts)).then(function (result) {
                if (result.success) {
                    utils.fixedAlert('success', '排序成功');
                }
                table.api().draw(false);
            });
        });

        //编辑开关
        var toggleHandler = utils.toggleChild(pane, 1, function (checked) {
            table.api().rows().every(function (idx) {
                var child = this.child, data = this.data();
                if (checked == 1) {
                    var opts = [
                        {text: '编辑字典', icon: 'fa-edit', className: 'edit-ope'},
                        {text: '删除字典', icon: 'fa-trash-o', className: 'delete-ope btn-danger'}
                    ];
                    if (data.feature) {
                        opts.push({text: '取消推荐', className: 'unfeature-opt btn-warning'});
                    } else {
                        opts.push({text: '启用推荐', className: 'feature-opt btn-success'});
                    }
                    child(utils.childWrapper(idx, opts)).show();
                } else {
                    child.hide();
                }
            });
        });

        //初始化事件
        table.on('draw.dt', toggleHandler).on('click', '.delete-ope', utils.tableEvent(table, function (idx, rowData) {
            //删除字典
            utils.confirmation(this, {
                placement: 'top',
                optName: '删除字典',
                title: '你确定删除字典?',
                fnConfirm: function () {
                    return adminApi('dicts_delete', {id: rowData.id}).then(utils.handDelete(table, {name: '字典'}));
                }
            });
        })).on('click', '.feature-opt', utils.tableEvent(table, function (idx, rowData) {
            //推荐字典
            return adminApi('dicts_edit', {
                id: rowData.id,
                feature: 1
            }).then(utils.handEdit(table, {idx: idx, name: '字典'}, true, toggleHandler));
        })).on('click', '.unfeature-opt', utils.tableEvent(table, function (idx, rowData) {
            //推荐字典
            return adminApi('dicts_edit', {
                id: rowData.id,
                feature: 0
            }).then(utils.handEdit(table, {idx: idx, name: '字典'}, true, toggleHandler));
        })).on('click', '.edit-ope', utils.tableEvent(table, function (idx, rowData) {
            //编辑字典
            utils.formEdit(table, {
                idx: idx, name: '字典', fields: comment_fields, columns: 6, data: rowData,
                fnServerData: function (paramData) {
                    return adminApi('dicts_edit', paramData);
                }
            });
        }));
    }
});